package com.intelligentcar.springboot.controller;

import com.intelligentcar.springboot.bean.Driversalary;
import com.intelligentcar.springboot.bean.News;
import com.intelligentcar.springboot.service.NewsService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

@Controller
public class NewsController {

    @Autowired
    NewsService newsService;


    @RequestMapping("/LookRad")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list(@RequestBody News news) {
        Map map = new HashMap();
        if (news.getInput().equals("") || news.getInput()==""){
            List<News> list = newsService.LookLook(news);
            map.put("list", list);
            map.put("total", newsService.selectNews());
        }
        for(int i=news.getInput().length();--i>=0;) {
            int chr = news.getInput().charAt(i);
            if (chr < 48 || chr > 57) {
                List<News> list = newsService.LookLook(news);
                map.put("list", list);
                map.put("total", newsService.selectNews());
            } else {
//                driversalary.setTime(driversalary.getInput());
                System.out.println(news.getInput());
                List<News> list = newsService.LookaddTime(news.getInput());
                map.put("list", list);
                map.put("total", newsService.selectNews());
            }
        }
        return map;
    }


    //    新闻公告界面数据展示
    @RequestMapping("/LookNews")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list() {
        List<News> list = newsService.Looknews();
//        System.out.println("213");
//       System.out.println(newsService.selectNews());
        Map map = new HashMap();
        map.put("list", list);
        map.put("total",newsService.selectNews());
//        System.out.println(list.get(0).getNewsMessage());
//        System.out.println(list.get(0).getNewsUrl());
//        System.out.println("1111111110000000000000000000000000000000++11111111111111111111");
        return map;
    }

    //    新闻公告界面中的修改信息操作
    @RequestMapping("/updNews")
    @ResponseBody //跳过视图解析器,直接返回数据
    public Map<String, Object> update(@RequestBody News news) {

//        System.out.println((news.getId()+"2222222222222"));
//        System.out.println(news.getNewsMessage()+"000000000000000000000000");
//        System.out.println(goods.getGoodsnum());
//        System.out.println(goods.getType());
        Map<String, Object> map = new HashMap<>();
        map.put("code", newsService.update(news));
        return map;
    }

    //新闻公告界面中的删除信息操作
    @RequestMapping("/delNews")//请求路径
    @ResponseBody //跳过视图解析器,直接返回数据
    public String delete(long id){
//        String id1 =intValue(id);;
        newsService.delete((int) id);
        System.out.println("id是:"+id);
        return "success";
    }

    //新闻公告界面中的新增信息操作
    @RequestMapping("/add")
    @ResponseBody //跳过视图解析器,直接返回数据
    public Map<String, Object> add(@RequestBody News news) {

        System.out.println((news.getId()+"2222222222222"));
        System.out.println(news.getNewsMessage()+"000000000000000000000000");
//        System.out.println(goods.getGoodsnum());
//        System.out.println(goods.getType());
        Map<String, Object> map = new HashMap<>();
        map.put("code", newsService.add(news));
        return map;
    }
}
